import {
  _decorator,
  Component,
  Node,
  resources,
  instantiate,
  Prefab,
} from "cc";
const { ccclass, property } = _decorator;
import { StateBase } from "../Base/StateBase";
import { BulletEnum } from "../Enum/BulletEnum";
import DataManager from "../Runtime/DataManager";
@ccclass("NormalBulletState")
export class NormalBulletState extends StateBase<BulletEnum> {
  constructor(prefab: Prefab) {
    super();
    this.prefab = prefab;
    this.player = DataManager.Instance.player;
    this.bulletParent = DataManager.Instance.bulletParent;
  }
  prefab: Prefab;
  player: Node;
  bulletParent: Node | null = null;
  private shootTimer: number = 0;
  private shootInterval: number = 1; // 射击间隔时间

  public get state(): BulletEnum {
    return BulletEnum.Normal;
  }
  public beforeEnter(): void {}
  public enter(): void {
    console.log("进入普通子弹状态");
  }

  public exit(): void {}

  public update(deltaTime: number): void {
    // 更新射击计时器
    this.shootTimer += deltaTime;
    if (this.shootTimer >= this.shootInterval) {
      this.shootTimer = 0;
      console.log("update普通子弹状态");
      const bulletNode = instantiate(this.prefab);
      if (bulletNode) {
        bulletNode.setPosition(this.player.position);
        bulletNode.setParent(this.bulletParent);
      }
    }
  }
}
